/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class FeedSyncItem : Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem
    {
        protected System.Xml.XmlElement m_XmlElement;
        protected System.Xml.XmlNamespaceManager m_XmlNamespaceManager;

        protected Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync m_Sync;

        public FeedSyncItem()
        {
        }

        public FeedSyncItem(System.Xml.XmlElement i_XmlElement, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            this.Initialize
                (
                i_XmlElement,
                i_XmlNamespaceManager
                );
        }

        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

        public System.Xml.XmlNamespaceManager XmlNamespaceManager
        {
            get
            {
                return m_XmlNamespaceManager;
            }
        }

        public virtual void Update(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.Update(i_WhenDateTime, i_By, true);
        }

        public virtual void ResolveConflicts(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.ResolveConflicts(i_WhenDateTime, i_By, true);
        }

        public virtual void Delete(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.Delete(i_WhenDateTime, i_By, true);
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync Sync
        {
            get
            {
                return m_Sync;
            }

            set
            {
                if (m_Sync != null)
                {
                    m_Sync.XmlElement.ParentNode.RemoveChild(m_Sync.XmlElement);

                    if (m_XmlElement.ChildNodes.Count == 0)
                        m_XmlElement.AppendChild(value.XmlElement);
                    else
                        m_XmlElement.InsertBefore(value.XmlElement, m_XmlElement.ChildNodes[0]);
                }

                m_Sync = value;
            }
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem CreateCopy()
        {
            Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem ClonedIFeedSyncItem = this.Clone();

            Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync Sync = new Sync
                (
                m_XmlElement.OwnerDocument,
                m_XmlNamespaceManager,
                "",
                System.Guid.NewGuid().ToString()
                );

            ClonedIFeedSyncItem.Sync = Sync;

            return ClonedIFeedSyncItem;
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem Clone()
        {
            System.Type FeedSyncItemType = this.GetType();
            Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem ClonedIFeedSyncItem = (Microsoft.CSA.FeedSync.FeedSyncLibrary.IFeedSyncItem)System.Activator.CreateInstance(FeedSyncItemType);

            ClonedIFeedSyncItem.Initialize
                (
                (System.Xml.XmlElement)m_XmlElement.CloneNode(true),
                m_XmlNamespaceManager
                );

            return ClonedIFeedSyncItem;
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync Initialize(System.Xml.XmlElement i_XmlElement, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            if (i_XmlElement == null)
                throw new System.ArgumentException("i_XmlElement must be non-null!");

            if (i_XmlNamespaceManager == null)
                throw new System.ArgumentException("i_XmlNamespaceManager  must be non-null!");

            bool Initialized =
                (m_XmlElement != null) ||
                (m_XmlNamespaceManager != null) ||
                (m_Sync != null);

            if (Initialized)
                throw new System.Exception("Already initialized!");

            m_XmlElement = i_XmlElement;
            m_XmlNamespaceManager = i_XmlNamespaceManager;

            m_Sync = new Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync(this);

            return m_Sync;
        }
    }
}
